'use strict';
// 修改密码通控制器模块
import {
    viewInit
} from '../views/changePasswordView.js';
var cw = require("../components/winLocation.js");
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var token, loginName, openid, loginType;
var $submitBtn, $oldPassword, $passwordNew, $passwordNew2;
var errorObject;
$(document).on("pageinit", "#changePassword_page", controllerInit);

// 控制器初始化
function controllerInit() {
    cw.checkBrowser();
    inintValue();
    viewInit();
    bindEvent();
}

// 请求修改密码
function changePassword() {
    if ($submitBtn.hasClass("weui-btn weui-btn_disabled")) {
        return;
    } else {
        var pwParments = getPwParments();
        if (pwParments.newPassWord != pwParments.newPassword2) {
            errorObject.showErroDialog("两次输入的密码不一致,请重新输入");
        } else {
            $.post(ModifyMerPw_URL, pwParments, pwback);
        }
    }
}

// 修改密码回调函数
function pwback(pwdata) {
    if (pwdata.login_response) {
        haslogin(pwdata);
        return;
    }
    console.dir(pwdata);
    if (pwdata.lfq_merextrainfo_response && pwdata.lfq_merextrainfo_response.resp_code == "0000") {
        errorObject.showErroDialog("修改密码成功,3秒后将登出");
        setTimeout(function() {
            logoutObject._removeSessionStorage();
            logoutObject.setLocalStorageLogout();
            location.href = LoginInit_URL;
        }, 3000);

    } else if (pwdata.lfq_merextrainfo_response && pwdata.lfq_merextrainfo_response.resp_code != "0000" && pwdata.lfq_merextrainfo_response.resp_msg) {
        errorObject.showErroDialog(pwdata.lfq_merextrainfo_response.resp_msg);
    } else if (pwdata.resp_msg) {
        errorObject.showErroDialog(pwdata.resp_msg);
    } else {
        errorObject.showErroDialog("修改密码失败");
    }
}

// 修改密码参数
function getPwParments(argument) {
    var oldPassword = $oldPassword.val();
    var newPassword = $passwordNew.val();
    var newPassword2 = $passwordNew2.val();
    var pwParments = {
        "oldPassWord": oldPassword,
        "newPassWord": newPassword,
        "newPassword2": newPassword2,
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType
    };

    return pwParments;
}

// 绑定事件
function bindEvent() {
    $submitBtn.on("touchstart", changePassword);
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}


// 初始化变量
function inintValue() {
    $oldPassword = $("#old-password");
    $passwordNew = $("#password_new");
    $passwordNew2 = $("#password_new2");
    $submitBtn = $("#submit_btn");
    getSessionStorage();
    errorObject = new errorDialog();
    logoutObject = new Logout();
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}